﻿#include "WidgetRoiUnion.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QInputDialog>
#include <QMenu>
#include <QWidgetAction>
#include <QStandardItem>
#include <QApplication>
#include <QtMath>
#include <QMessageBox>
#include "GraphicsScene.h"


#pragma execution_character_set("utf-8")
WidgetRoiUnion::WidgetRoiUnion(QWidget *parent)
    : QWidget{parent}
{
    setupUI();
    initConnection();
}

void WidgetRoiUnion::setImage(const QImage &img)
{
    mImg = img;
    mView->getView()->dispImage(mImg);
}

QList<RoiItemData> WidgetRoiUnion::getRoiList()
{
    int rowCount = mModelROI->rowCount();
    QList<RoiItemData> lisItem;
    lisItem.clear();
    for(int i = 0; i < rowCount; ++i){
        mLstItem.at(i)->setVisible(false);
        lisItem << mModelROI->item(i)->data().value<RoiItemData>();
    }
    return lisItem;
}


void WidgetRoiUnion::setupUI()
{
    mView = new WidgetRoiDraw();
    lstvwRoi = new WidgetRoiList();
    mModelROI = new QStandardItemModel();
    lstvwRoi->setModel(mModelROI);
    cbboxRegionAction = new QComboBox();
    cbboxRegionAction->addItem("选区相加", RA_Add);
    cbboxRegionAction->addItem("选区相减", RA_Sub);

    QHBoxLayout* hLayout = new QHBoxLayout();
    this->setLayout(hLayout);
    QVBoxLayout* vLayout = new QVBoxLayout();
    hLayout->addWidget(mView);
    hLayout->addLayout(vLayout);
    vLayout->addWidget(new QLabel("选区列表"));
    vLayout->addWidget(lstvwRoi);
    vLayout->addWidget(cbboxRegionAction);
    vLayout->addStretch();

}

void WidgetRoiUnion::initConnection()
{
    connect(lstvwRoi, &WidgetRoiList::sigRoiAdd, this, &WidgetRoiUnion::onRegionAdd);
    connect(lstvwRoi, &WidgetRoiList::sigRoiRemove, this, &WidgetRoiUnion::onRegionRemove);
    connect(lstvwRoi, &WidgetRoiList::sigRoiClear, this, &WidgetRoiUnion::onRegionClear);

    connect(lstvwRoi, &QListView::doubleClicked, this, [&](const QModelIndex& index){
        int row = index.row();
        mLstItem.at(row)->setVisible(!mLstItem.at(row)->isVisible());
    });
}
void WidgetRoiUnion::onRegionAdd()
{
    if(mImg.isNull())
        return;
    bool isOK;
    QString regionName = QInputDialog::getText(this,"请输入选区名称","选区名称",
                                               QLineEdit::Normal,"选区" + QString::number(mModelROI->rowCount()),
                                               &isOK);
    if(!isOK || regionName.isEmpty())
        return;
    ItemBase* item;
    float centerX = 0, centerY = 0;
    centerX = mImg.width() / 2;
    centerY = mImg.height() / 2;

    //在listview中添加项
    int index = mModelROI->rowCount();
    RoiItemData data;
    data.item = item;
    data.act = cbboxRegionAction->currentData().value<RegionAction>();
    QString iconPath = data.act == RA_Add ? ":/img/add.png" : ":/img/sub.png";
    QStandardItem* itemInsert = new QStandardItem(QIcon(iconPath), regionName);
    itemInsert->setData(QVariant::fromValue(data));
    mModelROI->insertRow(index, itemInsert);
}

void WidgetRoiUnion::onRegionRemove()
{
    int row = lstvwRoi->currentIndex().row();
    if(row == -1){
        QMessageBox::warning(this,"warning","未选择，请选择一个对象");
        return;
    }
    QStandardItem* itemIndex = mModelROI->item(row);
    mView->getView()->removeItem(itemIndex->data().value<RoiItemData>().item);
    mModelROI->removeRow(row);
    mLstItem.removeAt(row);
}

void WidgetRoiUnion::onRegionClear()
{
    int rowCount = mModelROI->rowCount();
    for(int i = 0; i < rowCount; ++i){
        QStandardItem* itemIndex = mModelROI->item(i);
        mView->getView()->removeItem(itemIndex->data().value<RoiItemData>().item);
    }
    mModelROI->removeRows(0, rowCount);
    mLstItem.clear();
}







